//
//  CXHInterstitialViewController.m
//  CXHAdSDK_Example
//
//  Created by 陈坤 on 2022/3/24.
//  Copyright © 2022 陈桑葚吃桑葚. All rights reserved.
//

#import "CXHInterstitialAdController.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <CXHAdSDK-Core/CXHAdSDKNativeInterstitialAd.h>

@interface CXHInterstitialAdController ()<CXHAdSDKNativeInterstitialAdDelegate>
@property (nonatomic) bool isAdLoaded;
@property (nonatomic) UIActivityIndicatorView *loadingView;
@property (nonatomic) CXHAdSDKNativeInterstitialAd *interAd;

@end

@implementation CXHInterstitialAdController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 在模态视图控制器中添加一个 loading 控件
    self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.loadingView.center = self.view.center;
    self.loadingView.color = [UIColor blackColor];
    [self.view addSubview:self.loadingView];
    [self.loadingView startAnimating];
}

- (void)viewDidAppear:(BOOL)animated{
    if (@available(iOS 14, *)) {
            [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                if ([NSThread isMainThread]) {
                    [self loadNativeInterstitialAd ];
                } else {
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        // 非主线程，将UI操作切换到主线程进行，请求广告
                        [self loadNativeInterstitialAd ];
                    });
                }
            }];
    } else {
        [self loadNativeInterstitialAd ];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"CXHInterstitialAd->touchesBegan...");
    if (self.interAd) {
        return;
    }
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (void)loadNativeInterstitialAd {
    NSLog(@"CXHInterstitialAd->loadNativeInterstitialAd...");
    if (self.interAd) {
        return;
    }
    // 其他相关的配置请参考文档
    self.interAd = [[CXHAdSDKNativeInterstitialAd alloc]init];
    self.interAd.delegate = self;
    self.interAd.controller = self;

    /// TODO 替换成自己的广告位
    [self.interAd loadAd:@"98855071"];
}


#pragma mark - CXHAdSDKNativeInterstitialAdDelegate
- (void)onCancelClicked:(id)sender {
    NSLog(@"CXHInterstitialAd->onCancelClicked...");
    self.interAd = nil;
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (void)nativeInterstitialAdSuccessToLoad:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdSuccessToLoad...");
    if (self.loadingView) {
        [self.loadingView stopAnimating];
        [self.loadingView removeFromSuperview];
        self.loadingView = nil;
    }
    [self.interAd showAdView];
}

- (void)nativeInterstitialAdExposure:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdExposure...");
}

- (void)nativeInterstitialAdClick:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdClick...");
}

- (void)nativeInterstitialAdClose:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdClose...");
    
    self.interAd = nil;
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (void)nativeInterstitialAdLoadingPageViewClose:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdLoadingPageViewClose...");
}

- (void)nativeInterstitialAdClick:(nonnull CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd loadingPageURL:(nonnull NSString *)loadingPageURL {
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdClick...");
}


- (void)nativeInterstitialAdFailedToLoad:(nonnull CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd error:(nonnull NSError *)error {
    NSLog(@"CXHInterstitialAd->nativeInterstitialAdFailedToLoad...");
    if (self.loadingView) {
        [self.loadingView stopAnimating];
        [self.loadingView removeFromSuperview];
        self.loadingView = nil;
    }
    self.interAd = nil;
    [self dismissViewControllerAnimated:YES completion:Nil];
}

@end
